import {Component, OnInit} from '@angular/core';
import {LogUtil} from '../../util/LogUtil';
import {NzModalService} from 'ng-zorro-antd';
import {GlobalService} from '../../service/GlobalService';
import {Constant} from '../../util/Constant';
import {ModelRes} from '../../entity/ModelRes';
import {ForgetPswComponent} from './forget-psw/forget-psw.component';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.less']
})
export class LoginComponent implements OnInit {

  phone = '';
  psw = '';

  constructor(public app: GlobalService, private modalService: NzModalService) {
  }

  ngOnInit() {
    this.phone = this.app.getUserInfo().phone;
  }

  login() {
    LogUtil.o('login');
    if (this.phone === '') {
      this.app.err('请输入账号');
      return;
    }
    if (this.psw === '') {
      this.app.err('请输入密码');
      return;
    }

    this.app.req(Constant.LOGIN, {'adminName': this.phone, 'adminPassword': this.psw}).then((resObj: ModelRes) => {
      const json = resObj;
      if (json.status === 200) {
        // this.app.ls.setObj('psw', this.psw);
        this.app.setUserInfo(json.data);
        this.app.router.navigate(['/main/admin']);
      }
    });


  }


  forget() {
    LogUtil.o('forget');

    const modal = this.modalService.create({
      nzTitle: '忘记密码',
      nzContent: ForgetPswComponent,
      nzComponentParams: {
        phone: this.phone,

      },
      nzFooter: [{
        label: '确定',
        shape: 'primary',
        onClick: (c) => {
          c.setPsw();

        }
      },
        {
          label: '取消',
          onClick: () => {
            modal.destroy();
          }

        }]
    });

  }


}
